import { reactive, watch } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
  const user = reactive({
    username: '',
    password: '',
    token: ''
  })
  // const doubleCount = computed(() => count.value * 2)
  // function increment() {
  //   count.value++
  // }
  const getUser = () => {
    return user
  }

  const setUser = (username, password, token) => {
    console.log(username, password, token);
    
    user.username = username
    user.password = password
    user.token = token
  }

  const removeUser = () => {
    user.username = ''
    user.password = ''
    user.token = ''
  }

  watch(
    user,
    (state) => {
      // const newVal = state
      // state.password = md5(state.password)
      // state.username = md5(state.username)
      // 每当状态发生变化时，将整个 state 持久化到本地存储。
      sessionStorage.setItem('userState', JSON.stringify(state))
    },
    { deep: true }
  )

  return { user, getUser, setUser, removeUser }
})
